<?php
class UserstudygroupController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('AuxDbTable');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Group');
        Zend_Loader::loadClass('Authgroup');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Authority');
        Zend_Loader::loadClass('Userstudygroup');
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }

    function indexAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'group','edit')) $this->_redirect(DEFAUL_URL);
        
        $group_id = (int)$this->_request->getParam('group', 0);
        $group = new Group();
        $this->view->group = $group->fetchRow('id='.$group_id);
        
        $this->view->title = "Group ".$this->view->group->name." : Students";
        $this->view->content_title = "Group ".$this->view->group->name." : Students";
        
        $links = new Userstudygroup();
        $this->view->linked = $links->getLinkedUser($group_id);
        $user = new User();
        $this->view->users = $user->fetchAll('deleted!=1 and authgroup_id=1 and id not in (select user_id from userstudygroup where studygroup_id="'.$group_id.'")','last_name');
        $this->view->content_title = "Group ".$this->view->group->name." : Students";
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            
            $selected = $this->_request->getPost('users');
            foreach($selected as $user_id){
                $links->LinkUser($group_id,$user_id);
            }
            $this->_redirect('/userstudygroup/index/group/'.$group_id);
            exit();
        }
        $this->view->can_add = $Rights->CanDo($this->view->user->id,'group','add');
        $this->view->can_edit = $Rights->CanDo($this->view->user->id,'group','edit');
        $this->view->can_delete = $Rights->CanDo($this->view->user->id,'group','delete');
        
        $this->render();
    }
    
    function deleteAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'group','edit')) $this->_redirect(DEFAUL_URL);
        
        $group_id = (int)$this->_request->getParam('group', 0);
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            $links = new Userstudygroup();
            $selected = $this->_request->getPost('linked');
            foreach($selected as $user_id){
                $links->UnlinkUser($group_id,$user_id);
            }
            $this->_redirect('/userstudygroup/index/group/'.$group_id);
            exit();
        }
        
    }
}